<template>
  <div>
    <div class="app-container">
      <div class="filter-container">
        <el-input
          v-model="keyword"
          placeholder="SN/MAC"
          style="width: 200px;"
          class="filter-item"
          @keyup.enter.native="handleFilter"
        />
      <el-select v-model="devType" clearable placeholder="设备类型" class="filter-item">
          <el-option
            v-for="item in deviceTypes"
            :key="item.value"
            :label="item.name"
            :value="item.value">
          </el-option>
        </el-select>
        <el-button class="filter-item" type="info" icon="el-icon-search" @click="resetQuery">重置</el-button>
        <el-button
          class="filter-item"
          type="primary"
          icon="el-icon-search"
          @click="handleFilter"
        >Search</el-button>
        <el-button class="filter-item" type="primary" icon="el-icon-plus" @click="addPackage">添加</el-button>
      </div>

      <el-table
        :key="tableKey"
        v-loading="listLoading"
        :data="list"
        border
        fit
        highlight-current-row
        style="width: 100%;"
        @sort-change="sortChange"
      >
        <el-table-column label="设备类型" class-name="status-col">
          <template slot-scope="{row}">
            <span>{{ row.devType }}</span>
          </template>
        </el-table-column>
        <el-table-column label="升级包名称" width="110px" align="center">
          <template slot-scope="{row}">
            <span>{{ row.packageName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="版本号" class-name="status-col">
          <template slot-scope="{row}">
            <span>{{ row.packageVersion }}</span>
          </template>
        </el-table-column>
        <el-table-column label="升级包地址" align="center">
          <template slot-scope="{row}">
            <span>{{ row.packageUrl }}</span>
          </template>
        </el-table-column>
        <el-table-column label="升级包长度" class-name="status-col">
          <template slot-scope="{row}">
            <span>{{ row.packageSize }}</span>
          </template>
        </el-table-column>
        <el-table-column label="升级包MD5" class-name="status-col">
          <template slot-scope="{row}">
            <span>{{ row.packageMd5 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="升级包解密key" class-name="status-col">
          <template slot-scope="{row}">
            <span>{{ row.decodeKey }}</span>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center">
          <template slot-scope="{row}">
            <span>{{ row.createDt | parseTime('{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="{row}">
            <el-button type="primary" size="mini" @click="showDetail(row)">Edit</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.limit"
        @pagination="getList"
      />
      <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
        <el-form
          ref="dataForm"
          :rules="rules"
          :model="temp"
          label-position="left"
          label-width="70px"
          style="width: 400px; margin-left:50px;"
        >
          <el-form-item label="设备类型" prop="devType">
            <el-select v-model="temp.devType" clearable placeholder="请选择">
              <el-option
                v-for="item in deviceTypes"
                :key="item.value"
                :label="item.name"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="升级包名称" prop="packageName">
            <el-input v-model="temp.packageName" />
          </el-form-item>
          <el-form-item label="升级包地址" prop="packageUrl">
            <el-input v-model="temp.packageUrl" />
          </el-form-item>
          <el-form-item label="升级包大小" prop="packageSize">
            <el-input v-model="temp.packageSize" />
          </el-form-item>
          <el-form-item label="升级包MD5值" prop="packageMd5">
            <el-input v-model="temp.packageMd5" />
          </el-form-item>
          <el-form-item label="升级包秘钥" prop="decodeKey">
            <el-input v-model="temp.decodeKey" />
          </el-form-item>
          <el-form-item label="升级包版本" prop="packageVersion">
            <el-input v-model="temp.packageVersion" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">Cancel</el-button>
          <el-button
            type="primary"
            @click="dialogStatus==='create'?createData():updateData()"
          >Confirm</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { listByPage, selectById, add, update } from "@/api/baseService";
import { getDictByType } from "@/api/dict";
import Sticky from "@/components/Sticky"; // 粘性header组件
import { parseTime } from "@/utils";
import Pagination from "@/components/Pagination"; // secondary package based on el-pagination

export default {
  name: "Upgrade",
  components: { Pagination, Sticky },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      keyword: null,
      devModel: "",
      devType: "",
      listQuery: {
        page: 1,
        limit: 20,
        params: null
      },
      dialogFormVisible: false,
      dialogStatus: "",
      deviceTypes:{},
      textMap: {
        update: "Edit",
        create: "Create"
      },
      rules: {
        // type: [
        //   { required: true, message: "type is required", trigger: "change" }
        // ],
        // timestamp: [
        //   {
        //     type: "date",
        //     required: true,
        //     message: "timestamp is required",
        //     trigger: "change"
        //   }
        // ],
        // title: [
        //   { required: true, message: "title is required", trigger: "blur" }
        // ]
      },
      temp: {
        packageName: null,
        packageUrl: null,
        packageSize: null,
        decodeKey: null,
        packageVersion: null,
        devType: null,
        createTime: null,
        packageMd5: null
      }
    };
  },
  created() {
    this.getList();
    getDictByType({"dictType":"device_type"}).then(response=>{
      this.deviceTypes = response.data;
    });
  },
  methods: {
    //返回列表页
    back2Main() {
      this.getList();
    },
    //获取列表
    getList() {
      this.listLoading = true;
      this.listQuery.params = {
        keyword: this.keyword,
        devType:this.devType
      };
      listByPage("upgrade/listByPage", this.listQuery).then(response => {
        this.list = response.result.rows;
        this.total = response.result.total;
        this.listLoading = false;
      });
    },
    handleFilter() {
      this.listQuery.page = 1;
      this.getList();
    },
    //重置
    resetQuery() {
      this.listQuery.page = 1;
      this.keyword = "";
      this.getList();
    },
    addPackage() {
      this.dialogStatus = "create";
      this.dialogFormVisible = true;
      this.temp = {};
    },
    sortChange(data) {
      const { prop, order } = data;
    },
    showDetail(row) {
      this.dialogStatus = "update";
      this.dialogFormVisible = true;
      var params = {
        packageId: row.packageId
      };
      selectById("upgrade/selectById", params).then(response => {
        this.temp = response.data;
      });
    },
    formatJson(filterVal) {
      return this.list.map(v =>
        filterVal.map(j => {
          if (j === "timestamp") {
            return parseTime(v[j]);
          } else {
            return v[j];
          }
        })
      );
    },
    createData() {
      this.temp;
      add("upgrade/add", this.temp).then(response => {
        console.log(response.data);
        this.dialogFormVisible = false;
        this.getList();
      });
    },
    updateData() {
      update("upgrade/edit", this.temp).then(response=>{
        this.dialogFormVisible = false;
        this.getList();
      });
    }
  }
};
</script>
<style lang="scss" scoped>
@import "~@/styles/mixin.scss";

.createPost-container {
  position: relative;

  .createPost-main-container {
    padding: 40px 45px 20px 50px;
    .postInfo-container {
      position: relative;
      @include clearfix;
      margin-bottom: 10px;
    }
  }

  .word-counter {
    width: 40px;
    position: absolute;
    right: 10px;
    top: 0px;
  }
}

.article-textarea ::v-deep {
  textarea {
    padding-right: 40px;
    resize: none;
    border: none;
    border-radius: 0px;
    border-bottom: 1px solid #bfcbd9;
  }
}
.divider {
  margin-left: 40px;
}
.row-title {
  font-size: 18px;
  color: #909399;
}
</style>